package com.xinke.module.system.service.notice;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.notice.*;
import com.xinke.module.system.model.entity.notice.NotifyMessageDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.notice.NotifyMessageMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 站内信消息 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class NotifyMessageServiceImpl implements NotifyMessageService {

    @Resource
    private NotifyMessageMapper notifyMessageMapper;

    @Override
    public Long createNotifyMessage(NotifyMessageSaveReqVO createReqVO) {
        // 插入
        NotifyMessageDO notifyMessage = BeanUtils.toBean(createReqVO, NotifyMessageDO.class);
        notifyMessageMapper.insert(notifyMessage);
        // 返回
        return notifyMessage.getMessageId();
    }

    @Override
    public void updateNotifyMessage(NotifyMessageSaveReqVO updateReqVO) {
        // 校验存在
        validateNotifyMessageExists(updateReqVO.getMessageId());
        // 更新
        NotifyMessageDO updateObj = BeanUtils.toBean(updateReqVO, NotifyMessageDO.class);
        notifyMessageMapper.updateById(updateObj);
    }

    @Override
    public void deleteNotifyMessage(Long messageId) {
        // 校验存在
        validateNotifyMessageExists(messageId);
        // 删除
        notifyMessageMapper.deleteById(messageId);
    }

    private void validateNotifyMessageExists(Long messageId) {
        if (notifyMessageMapper.selectById(messageId) == null) {
            throw exception(null);
        }
    }

    @Override
    public NotifyMessageDO getNotifyMessage(Long messageId) {
        return notifyMessageMapper.selectById(messageId);
    }

    @Override
    public PageResult<NotifyMessageDO> getNotifyMessagePage(NotifyMessagePageReqVO pageReqVO) {
        return notifyMessageMapper.selectPage(pageReqVO);
    }

}